package util;

import java.util.ArrayList;
import java.util.List;

public class PaginationHandler<T> {
	private int length = 5; //max Entries Per Page = 5 by default

	private List<T> list = new ArrayList<T>();
	
	public PaginationHandler(List<T> list) {
		if (list != null)
			this.list = list;
	}
	
	public void setLength(int length) {
		this.length = length;
	}
	
	public List<T> getList(int page) {

		List<T> temp = new ArrayList<T>();
		int offset = length * (page - 1);
		
		int to = offset + length;
		
		if (offset > list.size())
			offset = list.size();
		
		if (to > list.size())
			to = list.size();
		
		for (int i = offset; i < to; i++) {
			temp.add(list.get(i));
		}
		
		return temp;
	}
	
	public List<Integer> getPageNumbers() {
		List<Integer> pageNumbers = new ArrayList<Integer>();
		
		int pages = list.size() / length;
		
		if (list.size() % length != 0) {
			pages = pages + 1;
		}

		for (int i = 1; i <= pages; i++) {
			pageNumbers.add(new Integer(i));
		}
		
		return pageNumbers;
	}
	
	public Integer getListTotalNumbers() {
		return list.size();
	}
	
}
